//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Tathata.Common;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.BLL.Enums;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Localization;
using Tathata.Common.Utils;

namespace Tathata.Web.Modules.Products
{
    public partial class ProductVariantsInGridControl : BaseNopUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindData();
        }

        protected void BindData()
        {
            Product product = ProductManager.GetByProductID(ProductID);
            ProductVariantCollection productVariants = product.ProductVariants;
            if (product != null && productVariants.Count > 0)
            {
                if (productVariants.Count > 0)
                {
                    rptVariants.DataSource = productVariants;
                    rptVariants.DataBind();
                }
                else
                    rptVariants.Visible = false;
            }
            else
                Response.Redirect("~/Default.aspx");
        }

        protected void rptVariants_OnItemCommand(Object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart" || e.CommandName == "AddToWishlist")
            {
                NumericTextBox txtQuantity = e.Item.FindControl("txtQuantity") as NumericTextBox;
                Label productVariantID = e.Item.FindControl("ProductVariantID") as Label;
                SimpleTextBox txtTextOption = e.Item.FindControl("txtTextOption") as SimpleTextBox;
                ProductAttributesControl ctrlProductAttributes = e.Item.FindControl("ctrlProductAttributes") as ProductAttributesControl;
                Label lblError = e.Item.FindControl("lblError") as Label;

               
            }
        }

        protected void rptVariants_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ProductVariant productVariant = e.Item.DataItem as ProductVariant;

                Label lblPrice = e.Item.FindControl("lblPrice") as Label;
                Label lblOldPrice = e.Item.FindControl("lblOldPrice") as Label;
                Label lblDiscount = e.Item.FindControl("lblDiscount") as Label;
                Image iProductVariantPicture = e.Item.FindControl("iProductVariantPicture") as Image;
                Label lblTextOptionPrompt = e.Item.FindControl("lblTextOptionPrompt") as Label;
                SimpleTextBox txtTextOption = e.Item.FindControl("txtTextOption") as SimpleTextBox;
                NumericTextBox txtQuantity = e.Item.FindControl("txtQuantity") as NumericTextBox;
                Button btnAddToCart = e.Item.FindControl("btnAddToCart") as Button;
                Button btnAddToWishlist = e.Item.FindControl("btnAddToWishlist") as Button;

                if (iProductVariantPicture != null)
                {
                    Picture productVariantPicture = productVariant.Picture;
                    string pictureUrl = PictureManager.GetPictureUrl(productVariantPicture, 0, 0, false);
                    if (String.IsNullOrEmpty(pictureUrl))
                        iProductVariantPicture.Visible = false;
                    else
                        iProductVariantPicture.ImageUrl = pictureUrl;
                }

                if (lblPrice != null && lblOldPrice != null && lblDiscount != null)
                {
                    decimal oldPrice = productVariant.OldPrice;
                    decimal price = productVariant.Price;
                    decimal finalPriceWithoutDiscount = productVariant.FinalPriceWithoutDiscount;
                    decimal finalPriceWithDiscount = productVariant.FinalPriceWithDiscount;
                    decimal oldPriceConverted = CommonHelper.IIF(CurrencyManager.PrimaryStoreCurrency.CurrencyID == NopContext.Current.WorkingCurrency.CurrencyID, oldPrice, Decimal.Round(CurrencyManager.ConvertCurrency(oldPrice, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency), 2, MidpointRounding.AwayFromZero));
                    decimal priceConverted = CommonHelper.IIF(CurrencyManager.PrimaryStoreCurrency.CurrencyID == NopContext.Current.WorkingCurrency.CurrencyID, price, Decimal.Round(CurrencyManager.ConvertCurrency(price, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency), 2, MidpointRounding.AwayFromZero));
                    decimal finalPriceWithDiscountConverted = CommonHelper.IIF(CurrencyManager.PrimaryStoreCurrency.CurrencyID == NopContext.Current.WorkingCurrency.CurrencyID, finalPriceWithDiscount, Decimal.Round(CurrencyManager.ConvertCurrency(finalPriceWithDiscount, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency), 2, MidpointRounding.AwayFromZero));

                    if (priceConverted != oldPriceConverted && oldPriceConverted != decimal.Zero)
                    {
                        lblOldPrice.Text = LocalizationManager.GetCurrencyStringWithoutExchangeRate(oldPriceConverted, true, NopContext.Current.WorkingCurrency);
                        lblPrice.Text = LocalizationManager.GetCurrencyStringWithoutExchangeRate(priceConverted, true, NopContext.Current.WorkingCurrency);
                    }
                    else
                    {
                        lblOldPrice.Visible = false;
                        lblPrice.Text = LocalizationManager.GetCurrencyStringWithoutExchangeRate(priceConverted, true, NopContext.Current.WorkingCurrency);
                    }

                    if (finalPriceWithoutDiscount != finalPriceWithDiscount)
                        lblDiscount.Text = string.Format(GetLocaleResourceString("ProductVariantsInGridControlControl.DiscountIsApplied"), LocalizationManager.GetCurrencyStringWithoutExchangeRate(finalPriceWithDiscountConverted, true, NopContext.Current.WorkingCurrency));
                    else
                        lblDiscount.Visible = false;
                }

                if (!productVariant.DisableBuyButton)
                {
                    txtTextOption.ValidationGroup = string.Format("ProductVariant{0}", productVariant.ProductVariantID);
                    txtQuantity.ValidationGroup = string.Format("ProductVariant{0}", productVariant.ProductVariantID);
                    txtQuantity.RequiredErrorMessage = GetLocaleResourceString("ProductVariantsInGridControlControl.EnterQuantity");
                    txtQuantity.RangeErrorMessage = GetLocaleResourceString("ProductVariantsInGridControlControl.QuantityRange");
                    btnAddToCart.ValidationGroup = string.Format("ProductVariant{0}", productVariant.ProductVariantID);
                    btnAddToCart.Text = GetLocaleResourceString("Common.AddToCart");
                    btnAddToWishlist.ValidationGroup = string.Format("ProductVariant{0}", productVariant.ProductVariantID);
                    btnAddToWishlist.Text = GetLocaleResourceString("Common.AddToWishlist");
                }
                else
                {
                    txtQuantity.Visible = false;
                    btnAddToCart.Visible = false;
                    btnAddToWishlist.Visible = false;
                }

                if (productVariant.RequiresTextOption)
                {
                    txtTextOption.Visible = true;
                    txtTextOption.ErrorMessage = productVariant.TextOptionPrompt;
                    lblTextOptionPrompt.Visible = true;
                    lblTextOptionPrompt.Text = productVariant.TextOptionPrompt;
                }
                else
                {
                    txtTextOption.Visible = false;
                    lblTextOptionPrompt.Visible = false;
                }
            }
        }

        public int ProductID
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductID");
            }
        }
    }
}